home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 May / macformat-050.iso / Shareware Plus / Developers / Find_icon folder / Sources / Get_normal_folder_icon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-25  |  4.3 KB  |  138 lines  |  [TEXT/CWIE]

  1. /*    ---------------------------------------------------------------------------------------------
  2.     Find_icon, code for constructing icon suites for files and folders
  3.     
  4.     by James W. Walker
  5.     preferred e-mail: <mailto:jwwalker@kagi.com>
  6.     alternate e-mail: <mailto:jwwalker@aol.com>, <jim@nisus-soft.com>
  7.     web: <http://users.aol.com/jwwalker/>
  8.     
  9.     File: Get_normal_folder_icon.c
  10.     
  11.     Copyright ©1997 by James W. Walker
  12.     
  13.     You may incorporate this sample code into your applications without
  14.     restriction, though the sample code has been provided "AS IS" and the
  15.     responsibility for its operation is 100% yours.
  16.     If you're going to re-distribute the source, please make it clear
  17.     that the code was descended from James W. Walker's code,
  18.     but that you've made changes.
  19.     ---------------------------------------------------------------------------------------------
  20. */
  21.  
  22. #include <Icons.h>
  23. #include <Finder.h>
  24. #include <Folders.h>
  25. #ifndef __RESOURCES__
  26.     #include <Resources.h>
  27. #endif
  28. #include "Get_normal_folder_icon.h"
  29. #include "Get_resource_icons.h"
  30. #include "Copy_each_icon.h"
  31. #include "Find_icon.h"
  32.  
  33.  
  34. typedef struct    {OSType folder_type; long icon_id;} FolderPair;
  35.  
  36. static const FolderPair sFolderIconPair[] =
  37. {
  38.     { kExtensionFolderType, kExtensionsFolderIconResource },
  39.     //{ kSystemFolderType, kSystemFolderIconResource },    // special-cased
  40.     { kControlPanelFolderType, kControlPanelFolderIconResource },
  41.     { kFontsFolderType, kFontsFolderIconResource },
  42.     { kPreferencesFolderType, kPreferencesFolderIconResource },
  43.     { kAppleMenuFolderType, kAppleMenuFolderIconResource },
  44.     { kPrintMonitorDocsFolderType, kPrintMonitorFolderIconResource },
  45.     //{ kTrashFolderType, kTrashIconResource },    // trash is special, can be on any volume
  46.     { kStartupFolderType, kStartupFolderIconResource },
  47.     { kShutdownFolderType, kStartupFolderIconResource }
  48. };
  49.  
  50. /*    ------------------------------------------------------------------
  51.     Get_normal_folder_icon            Create an icon suite for a folder
  52.                                     that does not have a custom icon.
  53.     ------------------------------------------------------------------
  54. */
  55. OSErr    Get_normal_folder_icon(
  56. /* --> */    const CInfoPBRec    *cpb,
  57. /* --> */    MetaSelectorValue    icon_selector,
  58. /* <-- */    Handle    *the_suite
  59. )
  60. {
  61.     OSErr    err;
  62.     short    sys_vRefNum, trash_vRefNum, icon_id;
  63.     long    sys_dirID, trash_dirID, dirID;
  64.     short    save_resfile;
  65.     short        index;
  66.     short    found_vRefNum;
  67.     long    found_dirID;
  68.     IconSelectorValue    second_selector;
  69.     short    pairCount;
  70.     
  71.     ExpandMetaSelector( icon_selector, &icon_selector, &second_selector );
  72.  
  73.     dirID = cpb->dirInfo.ioDrDirID;
  74.     
  75.     if ( (FindFolder( cpb->dirInfo.ioVRefNum, kSystemFolderType, kDontCreateFolder,
  76.         &sys_vRefNum, &sys_dirID ) == noErr)
  77.         && (sys_vRefNum == cpb->dirInfo.ioVRefNum)
  78.         && (sys_dirID == dirID) )
  79.     {
  80.         icon_id = kSystemFolderIconResource;
  81.     }
  82.     else if ( (FindFolder( cpb->dirInfo.ioVRefNum, kTrashFolderType, kDontCreateFolder,
  83.         &trash_vRefNum, &trash_dirID ) == noErr)
  84.         && (trash_vRefNum == cpb->dirInfo.ioVRefNum)
  85.         && (trash_dirID == dirID) )
  86.     {
  87.         icon_id = kTrashIconResource;
  88.     }
  89.     else if ( (sys_vRefNum == cpb->dirInfo.ioVRefNum) &&
  90.         (sys_dirID == cpb->dirInfo.ioDrParID) )
  91.     {
  92.         /*
  93.             The folder is a subfolder of the System Folder, so it
  94.             might be one with a special icon.
  95.         */
  96.         icon_id = kGenericFolderIconResource;    // default
  97.         pairCount = sizeof(sFolderIconPair) / sizeof(FolderPair);
  98.         
  99.         for (index = 0; index < pairCount; ++index)
  100.         {
  101.             err = FindFolder( sys_vRefNum,
  102.                 sFolderIconPair[index].folder_type,
  103.                 kDontCreateFolder, &found_vRefNum, &found_dirID );
  104.             if ( (err == noErr) && (found_dirID == dirID) )
  105.             {
  106.                 icon_id = sFolderIconPair[index].icon_id;
  107.                 break;
  108.             }
  109.         }
  110.     }
  111.     else
  112.     {
  113.         /* These attribute bits are described in TN 301. */
  114.         if ((cpb->dirInfo.ioFlAttrib & 0x08) != 0)
  115.             icon_id = kMountedFolderIconResource;
  116.         else if ((cpb->dirInfo.ioFlAttrib & 0x20) != 0)
  117.             icon_id = kSharedFolderIconResource;
  118.         else if ((cpb->dirInfo.ioFlAttrib & 0x04) != 0)
  119.             icon_id = kOwnedFolderIconResource;
  120.         else
  121.             icon_id = kGenericFolderIconResource;
  122.     }
  123.     
  124.     save_resfile = CurResFile();
  125.     UseResFile(0);    // look in the System file
  126.     // The reason I use Get_resource_icons instead of Get1_resource_icons
  127.     // here is for compatibility with Aaron.
  128.     err = Get_resource_icons( the_suite, icon_id, icon_selector );
  129.     if ( (*the_suite == NULL) && (second_selector != 0) )
  130.     {
  131.         err = Get_resource_icons( the_suite, icon_id, second_selector );
  132.     }
  133.     
  134.     UseResFile( save_resfile );
  135.     
  136.     return err;
  137. }
  138.